﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using GoldenAgeHelper.common;

namespace GoldenAgeHelper.DAL
{
    public class FB
    {
        static FB()
        {

        }

        public string GOFB()
        {
            string text = "";

            return text;
        }
        public static event GA.DelShowMsg eveMsg;

        public static void Start(bool b)
        {
            int sleeptime = 500;
            string[] oids;
            if (b)
            {
                //进入郊外
                string text = GAClient.DownloadString(GA.ROOT_URL + "/environ?ts=" + Util.GetTime());
                AddMessage("已经进入郊外。");
                Thread.Sleep(sleeptime);
                //进入场景
                string text1 = GAClient.DownloadString(GA.ROOT_URL + "/envir25?ts=" + Util.GetTime());
                //http://t1.nexttime.cn/iview?oid=1&ts=1273291540890
                string oid = GARegex.GetString(text1, "<a href='#' u='/iview?oid=", "' class='s13' >");
                AddMessage("已经进入场景。");
                Thread.Sleep(sleeptime);

                //string oid = "1";
                //点击副本
                string text2 = GAClient.DownloadString(GA.ROOT_URL + "/iview?oid=" + oid + "&ts=" + Util.GetTime());
                AddMessage("点击副本，查看副本状态。");
                Thread.Sleep(sleeptime);
                //进入副本
                string text3 = GAClient.DownloadString( GA.ROOT_URL + "/isntf?oid=" + oid + "&ts=" + Util.GetTime());
                //查找可以打的NPC
                oids = GARegex.GetArrstr(text3, "<a href='#' u='/iscbf?oid=", "'>");
                AddMessage("已经进入副本，发现NPC，数量为：" + oids.Length + ".");
                Thread.Sleep(sleeptime);
            }
            else
            {
                //直接进入场景
                string text = GAClient.DownloadString(GA.ROOT_URL + "/instance?cid=11&ts=" + Util.GetTime());
                AddMessage("直接进入场景。");
                //一共有 7 道关卡
                //第 <strong class='s2'>3</strong> 道关卡：
                //<div class='ctlt'><h3>【兰州】郊外 〉试练洞</h3></div>
                string fbname = GARegex.GetString(text, "<div class='ctlt'><h3>", "</h3></div>");
                string fbcount = GARegex.GetString(text, "一共有 ", " 道关卡");
                string fbcurrent = GARegex.GetString(text, "第 <strong class='s2'>", "</strong> 道关卡：");
                AddMessage("进入副本【" + fbname + "】，当前第" + fbcurrent + "/" + fbcount + "道关卡。");
                //查找可以打的NPC
                //<a href='#' u='/iscbf?oid=185110'>
                oids = GARegex.GetArrstr(text, "<a href='#' u='/iscbf?oid=", "'>");
                Thread.Sleep(sleeptime);
            }
            while (true)
            {
                bool bnext = true;
                for (int i = 0; i < oids.Length; i++)
                {
                    //打FB怪物
                    string text4 = GAClient.DownloadString( GA.ROOT_URL +"/iscbf?oid=" + oids[i] + "&ts=" + Util.GetTime());

                    string msg = "";
                    AddMessage("正在打第" + (i + 1) + "个副本怪物，id=" + oids[i]);

                    bnext = text4.IndexOf("退出副本") > 0;

                    //休息时间5秒
                    Thread.Sleep(5000);
                    UpdateUser();
                }

                /*进入下一层*/
                string str = GAClient.DownloadString(GA.ROOT_URL + "/isntf?ts=" + Util.GetTime());
                AddMessage("进入副本下一层。");
                if (bnext)
                {
                    AddMessage("副本已经打完，退出副本。");
                    break;
                }
                oids = GARegex.GetArrstr(str, "<a href='#' u='/iscbf?oid=", "'>");
            }

        }

        private static void AddMessage(string msg)
        {
            if (eveMsg!=null)
            {
                eveMsg(msg);
            }
        }

        //更新用户信息，如打副本时，需要即时更新
        private static void UpdateUser()
        {
            string text = GAClient.DownloadString(GA.ROOT_URL + "/ucrv?ts=" + Util.GetTime());
        }
    }
}
